// pages/order-detail/order-detail.js
const SERVICE = require("../../service.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    stautsText:"去支付"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId
    });
    orderDetail(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  ringUp(e) {
    let mobile = e.currentTarget.dataset.mobile;
    let td = this.data;
    let openid = wx.getStorageSync("token");
    
    if (td.orderDetail.info.pay_status == 2){
      wx.showModal({
        title: "",
        content: "客服热线" + mobile,
        confirmColor: "#31AEFE",
        success: function (res) {
          if (res.confirm) {
            wx.makePhoneCall({
              phoneNumber: mobile,
              success: function () {
                console.log("拨打电话成功！")
              },
              fail: function () {
                console.log("拨打电话失败！")
              }
            });
          }
        }
      })
    }else{
      wx.showModal({
        title: "",
        content: "是否取消订单",
        success:function(res){
          if(res.confirm){
            SERVICE.cellOrder({ order_id: td.orderDetail.info.order_id, openid }, (status, data) => {
              console.log(data);
              wx.showToast({
                title: data.message,
                icon: "none",
                mask: true
              });
              setTimeout(() => {
                wx.navigateBack({});
              }, 2000);
            });
          }
        }
      })
    }
  },
  button(e){
    let _this = this;
    let td = this.data;
    let status = parseInt(e.currentTarget.dataset.status);
    console.log(status);
    let order_id = e.currentTarget.dataset.id;
    let openid = wx.getStorageSync("token");
    if (status == 2){
      wx.showModal({
        title: '',
        content: '确认已收到商品?',
        success(res) {
          if (res.confirm) {
            SERVICE.Receiving({ openid, order_id }, (status, data) => {
              wx.showToast({
                title: data.message,
                icon: "none",
                mask: true
              });
              setTimeout(() => {
                wx.navigateBack({});
              }, 2000);
            });
          }
        }
      })
      
    } else if (status == 0){
      wx.navigateTo({
        url: '../pay/pay?type=1&orderId=' + td.orderDetail.info.order_id,
      });
    }
  }
})

function orderDetail(e){
  let openid = wx.getStorageSync("token");  
  wx.showNavigationBarLoading();
  SERVICE.getOrderDetail({ order_id: e.data.orderId, openid }, (status, data) => {
    wx.hideNavigationBarLoading();
    console.log("订单数据:",data);
    if (status) {
      let stauts_text = "";
      if (data.data.info.pay_status == 2){
        stauts_text = "确认收货";
      }else{
        stauts_text = "去支付";
      }
      e.setData({
        orderDetail: data.data,
        stautsText: stauts_text
      });
    }

  });
}